<template lang="">
  <div id="home" ref="home">
      <!-- nav -->
      <nav-bar class="home-nav"><div slot="center">购物街</div></nav-bar>
      <!-- tabControl copy-->
      <tab-control
        :titles="['流行', '新品', '精选']"
        @tab-click="tabClick"
        ref="tabControl1"
        v-show="isTabFixed"
        class="tab-control"
      ></tab-control>
      <!-- scroll-component -->
      <scroll
        class="content"
        ref="scroll"
        :probeType="3" 
        :pullUpLoad="true"
        @scroll="scroll"
        @pullingUp="pullingUp"
      >
        <!-- banner -->
        <home-swiper :banners="banners"></home-swiper>
        <!-- recommend -->
        <recommend-view :recommends="recommends"></recommend-view>
        <!-- featureView -->
        <feature-view></feature-view>
        <!-- tabControl -->
        <tab-control
          :titles="['流行', '新品', '精选']"
          @tab-click="tabClick"
          ref="tabControl2"
          v-show="!isTabFixed"
        ></tab-control>
        <!-- goodsList -->
        <goods-list :goods="showGoods"></goods-list>
      </scroll>
      <!-- backTop -->
      <back-top @click.native="backTop" v-show="isShowBackTop"></back-top>
  </div>
</template>
<script>
  import HomeSwiper from '@/views/home/childComps/HomeSwiper'
  import RecommendView from '@/views/home/childComps/RecommendView'
  import FeatureView from '@/views/home/childComps/FeatureView'

  import GoodsList from '@/components/content/goods/GoodsList'
  import TabControl from '@/components/content/tabControl/TabControl'
  import NavBar from '@/components/common/navbar/NavBar'
  import Scroll from '@/components/common/scroll/Scroll'
  import BackTop from '@/components/content/backTop/BackTop'

  import { getHomeMultidata, getHomeGoods } from '@/network/home'
  import { debounce } from '@/common/utils'
  import { backTopMixin } from "@/common/mixin.js"


  export default {
    name: 'Home',
    components: {
      HomeSwiper,
      RecommendView,
      FeatureView,
      GoodsList,
      TabControl,
      NavBar,
      Scroll,
      BackTop
    },
    mixins: [backTopMixin],
    data() {
      return {
        banners: [],
        recommends: [],
        goods: {
          'pop': {page:0, list: []},
          'new': {page:0, list: []},
          'sell': {page:0, list: []}
        },
        currentType: 'pop',
        isShowBackTop: false,
        tabOffsetTop: 614,
        isTabFixed: false,
        saveY: 0
      }
    },
    computed: {
      showGoods() {
        return this.goods[this.currentType].list
      }
    },
    created() {
      // 请求多个数据
      this.getHomeMultidata()
      // 请求商品数据
      this.getHomeGoods('pop')
      this.getHomeGoods('new')
      this.getHomeGoods('sell')
    },
    mounted() {
      // 监听图片刷新 防抖
      const refresh = debounce(this.$refs.scroll.refresh, 200)
      this.$bus.$on('itemImageLoad', () => {
        refresh()
      })
    },
    activated() {
      this.$refs.scroll.scrollTo(0, this.saveY, 0)
      this.$refs.scroll.refresh()
    },
    deactivated() {
      this.saveY = this.$refs.scroll.getScrollY()
    },
    methods: {
      /**
       * 事件监听相关方法
       */
      tabClick(index) {
        switch (index) {
          case 0:
            this.currentType = 'pop'
            break;
          case 1:
            this.currentType = 'new'
            break;
          case 2:
            this.currentType = 'sell'
        }
        this.$refs.tabControl1.currentIndex = index
        this.$refs.tabControl2.currentIndex = index
      },
      scroll(position) {
        this.listenShowBackTop(position);
        this.isTabFixed = (-position.y) > this.tabOffsetTop
      },
      pullingUp() {
        this.getHomeGoods(this.currentType)
      },

      /**
       * 网络请求相关方法
       */
      getHomeMultidata() {
        getHomeMultidata().then( res => {
          console.log(res)
          this.banners = res.data.banner.list
          this.recommends = res.data.recommend.list
        }) 
      },
      getHomeGoods(type) {
        const page = this.goods[type].page + 1
        getHomeGoods(type, page).then( res => {
          this.goods[type].page += 1
          this.goods[type].list.push(...res.data.list)
          this.$refs.scroll.finishPullUp()
        })
      }
    }
  }
</script>
<style scoped>
  #home {
    padding-top: 44px;
    height: 100vh;
    position: relative;
  }
  .home-nav {
    background-color: var(--color-tint);
    color: #fff;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9;
  }

  .content {
    overflow: hidden;
    position: absolute;
    top: 44px;
    bottom: 49px;
    overflow: hidden;
  }
  .tab-control {
    position: fixed;
    left: 0;
    right: 0;
    z-index: 9;
  }
</style>